package com.ccod.mapper.spring;

import com.ccod.mapper.mapper.core.MyMapperRefresh;
import com.ccod.mapper.mapper.core.chain.CoreServiceChain;
import com.ccod.mapper.mapper.core.chain.CoreServiceProcess;
import com.ccod.mapper.mapper.core.model.MapperRefreshContext;
import com.ccod.mapper.spring.utils.ResourceResolveUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

import java.io.File;
import java.util.List;

/**
 * @author ccod
 * @date 2020-12-06 12:45
 **/
public class MapperRefreshFactoryBean implements InitializingBean, DisposableBean,FactoryBean<MyMapperRefresh> {

    private String[] mapperLocations;

    /**
     * 延迟加载时间
     */
    private int delaySeconds = 10;

    /**
     * 刷新间隔时间
     */
    private int sleepSeconds = 20;

    /**
     * 是否开启自动刷新功能
     */
    private boolean enabledRefresh;

    private SqlSessionFactory sqlSessionFactory;

    private MyMapperRefresh myMapperRefresh;

    private CoreServiceChain<MapperRefreshContext> coreServiceChain = new CoreServiceChain();

    private List<CoreServiceProcess> serviceProcessList;

    @Override
    public void afterPropertiesSet() throws Exception {
        File[] mapperFiles = ResourceResolveUtils.resolveMapperFiles(mapperLocations);
        coreServiceChain.setServiceProcessList(serviceProcessList);
        this.myMapperRefresh = new MyMapperRefresh(
                sqlSessionFactory, mapperFiles, coreServiceChain, enabledRefresh
        );
        myMapperRefresh.setDelaySeconds(delaySeconds);
        myMapperRefresh.setSleepSeconds(sleepSeconds);
        myMapperRefresh.run();
    }

    public String[] getMapperLocations() {
        return mapperLocations;
    }

    public void setMapperLocations(String[] mapperLocations) {
        this.mapperLocations = mapperLocations;
    }

    public int getDelaySeconds() {
        return delaySeconds;
    }

    public void setDelaySeconds(int delaySeconds) {
        this.delaySeconds = delaySeconds;
    }

    public int getSleepSeconds() {
        return sleepSeconds;
    }

    public void setSleepSeconds(int sleepSeconds) {
        this.sleepSeconds = sleepSeconds;
    }

    public boolean isEnabledRefresh() {
        return enabledRefresh;
    }

    public void setEnabledRefresh(boolean enabledRefresh) {
        this.enabledRefresh = enabledRefresh;
    }

    public SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }

    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    public void setServiceProcessList(List<CoreServiceProcess> serviceProcessList) {
        this.serviceProcessList = serviceProcessList;
    }

    @Override
    public MyMapperRefresh getObject() throws Exception {
        if(myMapperRefresh == null){
            afterPropertiesSet();
        }
        return myMapperRefresh;
    }

    @Override
    public Class<?> getObjectType() {
        return myMapperRefresh == null ? MyMapperRefresh.class : myMapperRefresh.getClass();
    }

    @Override
    public void destroy() throws Exception {
        this.myMapperRefresh.shutdown();
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
